<template>
	<div id="wangeditor">
		<div ref="editorElem"></div>
	</div>
</template>

<script>
import E from 'wangeditor'

export default {
	data () {
		return {
			editorObj: null
		}
	},
	props: ['edit'], // 接收父组件的方法
	mounted () {
		this.init()
	},
	methods: {
		init () {
			const editor = new E(this.$refs.editorElem) // 创建富文本实例
			editor.config.height = 500
			editor.config.onchange = (html) => {
				this.$emit('edit', encodeURI(html.replace(/&nbsp;/g, ' '))) // 把这个html通过catchData的方法传入父组件
			}
			editor.create()
			this.editorObj = editor
		},
		setContent (val, type) {
			if (type) {
				// 设置
				let txt = decodeURI(val).replace(/&amp;nbsp;/g, ' ')
				this.editorObj.txt.html(txt) // 重新设置编辑器内容
			} else {
				// 清空
				this.editorObj.txt.clear()
			}
		}
	}
}
</script>

<style>
</style>
